import 'package:flutter/material.dart';
import 'package:flutter_wan_android/bean/project_list_bean.dart';
import 'package:flutter_wan_android/bean/project_tree_bean.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:flutter_wan_android/blocs/project_bloc.dart';
import 'package:flutter_wan_android/blocs/project_tree_bloc.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/dimens.dart';
import 'package:flutter_wan_android/res/strings.dart';
import 'package:flutter_wan_android/ui/project/project_list_page.dart';
import 'package:flutter_wan_android/widgets/empty_widget.dart';
import 'package:flutter_wan_android/widgets/home_widgets.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ProjectPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ProjectPageState();
  }
}

class _ProjectPageState extends State<ProjectPage>
    with SingleTickerProviderStateMixin {
  ProjectTreeBloc _bloc;
  TabController _tabController;
  List<BlocProvider<ProjectBloc>> _listChild=[];

  @override
  void initState() {
    super.initState();
    _bloc = BlocProvider.of<ProjectTreeBloc>(context);
    _bloc.getProjectTree();
  }



  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<ProjectTreeBean>>(
        stream: _bloc.stream,
        builder: (context, AsyncSnapshot<List<ProjectTreeBean>> snapshot) {
          if (snapshot.hasError) {
            return ErrorView(
              onTap: () {
                _bloc.getProjectTree();
              },
            );
          }

          if (!snapshot.hasData) {
            return LoadingView();
          }

          if (_tabController == null) {
            _tabController =
                TabController(length: snapshot.data.length, vsync: this);

          }

          if(_listChild.isEmpty){
            _listChild= snapshot.data.map((item){
              return BlocProvider<ProjectBloc>(bloc: ProjectBloc(), child: ProjectListPage(cid: item.id,));
            }).toList();

          }


          return Scaffold(
            appBar: PreferredSize(
                child: AppBar(
                  title: TabBar(
                      controller: _tabController,
                      isScrollable: true,
                      labelColor: XColors.white,
                      //选中的颜色
                      labelStyle: TextStyle(fontSize: 16),
                      //选中的样式
                      unselectedLabelColor: XColors.mainTabUnSelectedColor,
                      //未选中的颜色
                      unselectedLabelStyle: TextStyle(fontSize: 14),
                      //未选中的样式
                      indicatorColor: XColors.white,
                      tabs: snapshot.data.map((item) {
                        return Tab(
                          text: item.name,
                        );
                      }).toList()),
                ),
                preferredSize: Size.fromHeight(XDimens.appBarHeight)),
            body: TabBarView(
                controller: _tabController,
                children: _listChild),
          );
        });

  }







}



